/**
 * 小数位数
 */

import store from '@/store'

/**
 * 价格小数位数
 */
const priceDigit = (price) => {
  const price_decimal = store.getters.getSystemSetByName('price_decimal')

  if (price === '') return
  return Number(price).toFixed(price_decimal)
}

/**
 * 数量小数位数
 */
const numberDigit = (number) => {
  const number_decimal = store.getters.getSystemSetByName('number_decimal')

  if (number === '') return ''
  return Number(number).toFixed(number_decimal)
}

/**
 * 价格/数量输入框失去焦点
 * @param e 输入框失去焦点事件
 */
const digitBlur = (e, type) => {
  const { value } = e.detail
  const price_decimal = store.getters.getSystemSetByName('price_decimal')
  const number_decimal = store.getters.getSystemSetByName('number_decimal')

  if (value === '') return ''
  if (type === 'price') {
    return Number(value).toFixed(price_decimal)
  } else if (type === 'number') {
    return Number(value).toFixed(number_decimal)
  }
}

/**
 * 率输入框失去焦点
 * @param number 输入框的值
 */
const rateBlur = (rate) => {
  if (rate) return ''
  return Number(rate).toFixed(2)
}

export default {
  priceDigit,
  numberDigit,
  digitBlur,
  rateBlur
}
